<template>
  <div class="">
    <div class="warp-box  clearfix">
      <div class="warp-con">
        <div class="header">
          <div class="headerWrapper">
            <div class="search">
              <el-form :inline="true" class="demo-form-inline">
                <el-form-item>
                  <el-input @keyup.enter.native="query" auto-complete="off" class="input" placeholder="请输入名称"
                            type="text" v-model="name">
                    <i class="el-icon-close el-input__icon" slot="suffix" @click="name = ''"></i>
                  </el-input>
                </el-form-item>
                <el-form-item>
                  <el-button style="margin-left: 5px" @click="query" type="primary">查询</el-button>
                </el-form-item>
                <el-form-item>
                  <el-button @click="addDialog()" type="primary">新增</el-button>
                </el-form-item>
              </el-form>
            </div>
          </div>
        </div>
        <div class="content">
          <el-table ref="multipleTable" :data="tableData" border max-height="635" :max-height="635"
                    :tree-props="{children: 'children', hasChildren: 'hasChildren'}" :tooltip-effect="'dark'"
                    element-loading-text="拼命加载中" element-loading-spinner="el-icon-loading"
                    element-loading-background="rgba(0, 0, 0, 0.8)" style="width: 100%" row-key="id">
            <el-table-column align="center" prop="createDate" show-overflow-tooltip label="创建时间"/>
            <el-table-column align="center" prop="name" show-overflow-tooltip label="名称"/>
            <el-table-column align="center" prop="sortNo" show-overflow-tooltip label="排序"/>
            <el-table-column label="操作" align="center" min-width="130" width="">
              <template slot-scope="scope">
                <span v-if="!scope.row.parentId && scope.row.type === 'select'" @click="addDialog(scope.row)"
                      class="pointer">新增<i
                  class="line"></i></span>
                <span @click="updateDialog(scope.row.id)" class="pointer">修改</span>
                <i class="line"></i>
                <span @click="removeItem(scope.row.id)" class="pointer">删除</span>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </div>
      <el-dialog :close-on-click-modal="false" :title="detailEditType == 'edit' ? '修改采集项' : '添加采集项'" width="650px"
        :before-close="cancel" :visible.sync="dialogFormVisible">
        <el-form :model="addForm" :rules="rulesAdd" ref="ruleAddForm" class="demo-ruleForm" label-width="120px">
          <div class="form-table_col2">
            <table border="1" width="100%">
              <tr>
                <td class="form-item_label">名称</td>
                <td>
                  <el-input v-model="addForm.name" class="input"></el-input>
                </td>
              </tr>
              <tr>
                <td>字段KEY</td>
                <td>
                  <el-input v-model="addForm.fieldKey" class="input"></el-input>
                </td>
              </tr>
              <tr>
                <td class="form-item_label">字段类型</td>
                <td>
                  <el-select filterable v-model="addForm.type" @change="typeChange"  :disabled="disabled">
                    <el-option v-for="item in typeOptions" :key="item.value" :label="item.label" :value="item.value" />
                  </el-select>
                </td>
              </tr>
              <tr v-if="addForm.type === 'select' && addForm.parentId != null">
                <td>上级</td>
                <td>
                  <el-select filterable v-model="addForm.parentId" @change="changeValue" placeholder="请选择" clearable>
                    <el-option v-for="item in parents" :key="item.id" :label="item.name" :value="item.id">
                    </el-option>
                  </el-select>
                </td>
              </tr>
              <tr>
                <td>排序</td>
                <td>
                  <el-input-number v-model="addForm.sortNo" :min="1" class="input"></el-input-number>
                </td>
              </tr>
            </table>
          </div>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="saveTemplateItem('ruleAddForm')" class="dialogBtn">保 存</el-button>
          <el-button type="primary" @click="cancel" class="dialogBtn">取 消</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>
<script>
  import templateApi from '@/api/templateApi'

  export default {
    data() {
      return {
        loading: false,
        name: null,
        tableData: [],
        pageIndex: 1,
        pageSize: 10,
        TableDataTotalCount: '',
        dialogFormVisible: false,
        detailEditType: 'detail', // 'edit'
        addForm: {},
        disabled: false,
        typeOptions: [{
            label: '文本',
            value: 'input'
          },
          {
            label: '数字',
            value: 'number'
          },
          {
            label: '日期',
            value: 'data'
          },
          {
            label: '文件',
            value: 'upload'
          },
          {
            label: '下拉框',
            value: 'select'
          }
        ],
        rulesAdd: {
          name: [{
            required: true,
            message: '请输入采集项名称',
            trigger: 'blur'
          }],
          type: [{
            required: true,
            message: '请选择字段类型',
            trigger: 'blur'
          }]
        },
        parents: []
      }
    },
    created() {
      this.query();
    },
    mounted() {},
    methods: {
      query() {
        this.loading = true;
        this.tableData = [];
        templateApi.findItem({name:this.name}).then((res) => {
          this.loading = false;
          if (res.data.messageCode === '200') {
            this.tableData = res.data.result;
          }
        })
      },
      addDialog(row) {
        if (row) {
          this.disabled = true;
          this.addForm.type = row.type;
          this.addForm.parentId = row.id;

          let sort = 1;
          let children = row.children;
          if (children && children.length > 0) {
            sort = children.length + 1;
          }
          this.addForm.sortNo = sort;
        } else {
          this.addForm.sortNo = this.tableData.length + 1;
        }

        this.detailEditType = 'add';
        this.dialogFormVisible = true;
      },
      updateDialog(param) {
        this.findTemplateItem()
        this.detailEditType = 'edit'
        this.dialogFormVisible = true
        templateApi.getTemplateItem(param).then((res) => {
          if (res.data.messageType === '0') {
            this.addForm = res.data.result
            if (this.addForm.type === 'select') {
              this.disabled = true
            }
          }
        })
      },
      findTemplateItem() {
        templateApi.findTemplateItem({
          type: 'select'
        }).then((res) => {
          if (res.data.messageType === '0') {
            this.parents = res.data.result;
          }
        })
      },
      saveTemplateItem(formName) {
        if (!this.addForm.name) {
          this.$message({
            message: '请输入名称',
            type: 'warning'
          })
          return false
        }
        if (!this.addForm.type) {
          this.$message({
            message: '请输入字段类型',
            type: 'warning'
          })
          return false
        }
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.showLoading()
            let params = Object.assign({}, this.addForm)
            templateApi.saveTemplateItem(params).then((res) => {
              this.hideLoading()
              if (res.data.messageType === '0') {
                this.query();
                this.$notify({
                  title: '消息',
                  message: ('保存成功!'),
                  position: 'bottom-right',
                  showClose: false,
                  type: 'success'
                })
                this.cancel();
              }
            })
          } else {
            return false
          }
        })
      },
      removeItem(param) {
        let tips = '确定要删除吗？'
        this.$confirm(tips, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.showLoading()
          templateApi.deleteTemplateItem(param).then((res) => {
            this.hideLoading()
            if (res.data.messageType === '0') {
              this.$notify({
                title: '消息',
                message: ('删除成功！'),
                position: 'bottom-right',
                showClose: false,
                type: 'success'
              })
              this.query();
            }
          })
        }).catch((error) => {
          console.log(error)
        })
      },
      cancel() {
        this.addForm = {}
        this.disabled = false
        this.dialogFormVisible = false
      },
      typeChange(val) {
        if (val && val === 'select') {
          this.findTemplateItem();
        }
      },
      changeValue() {
        if (this.addForm.parentId === this.addForm.id) {
          this.addForm.parentId = null
          this.$message.error('不能选择自己')
        }
      },
      showLoading() {
        this.$loading({
          lock: true,
          text: 'Loading',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        })
      },
      hideLoading() {
        this.$loading().close()
      }
    }
  }
</script>
<style lang="less" scoped>
  .header {
    height: 60px;

    .headerWrapper {
      display: inline-flex;
    }

    .btn {
      cursor: pointer;
      border: none;
      padding: 10px;
      font-size: 16px;
      font-weight: 400;
      color: #ffffff;
      background: rgba(14, 107, 183, 0.9);
      margin-left: 18px;
    }

    .search {
      font-size: 22px;
      font-weight: 400;
      color: rgba(255, 255, 255, 1);

      .input {
        width: 243px;
        height: 47px;
        background: rgba(0, 0, 0, 0);
        font-size: 18px;
        color: #ffffff;
      }
    }
  }

  .container {
    padding-left: 94px;
    padding-right: 94px;
  }

  .demo-ruleForm {
    & /deep/ .el-input--suffix .el-input__inner {
      padding-right: 15px;
    }
  }

  .f-g-sd1 {
    position: relative;
    float: left;
    width: 95px;
    margin-right: -95px;
  }

  .f-g-mn1 {
    float: right;
    width: 100%;
  }

  .f-g-mn1c {
    margin-left: 100px;
  }

  .detail-body {
    color: rgba(250, 250, 250, 0.9);

    &::after {
      content: '';
      display: block;
      visibility: hidden;
      height: 0;
      clear: both;
    }
  }

  .item-inline {
    width: 50%;
    float: left;
    margin-bottom: 20px;

    .item-name {
      display: inline-block;
    }

    .item-context {
      display: inline-block;
    }
  }

  .no-data {
    text-align: center;
    font-size: 14px;

    .tips {
      padding: 20px 0px;
      color: #666;
    }
  }

  .el-form-item__label {
    padding: 0 20px 0 0 !important;
  }

  .footer {
    margin-top: 40px;
    color: #ffffff;
    font-size: 16px;

    .info {
      .select {
        width: 52px;
        height: 35px;
      }

      .select /deep/ .el-input__inner {
        padding-left: 5px;
        padding-right: 0;
      }

      .select /deep/ .el-input__icon {
        width: 15px;
      }

      .select /deep/ .el-select__caret {
        /*transform:rotate(270deg);*/
        /*color: black;*/
      }

      .select /deep/ .el-select__caret.is-reverse {}
    }

    .paging {
      /*background-color: pink;*/
      text-align: right;
      padding-right: 30px;
      margin-top: -20px;

      & /deep/ .el-pagination.is-background .btn-prev {
        background: none;
        border: 1px solid rgba(0, 255, 255, 1);
      }

      & /deep/ .el-pagination.is-background .btn-next {
        background: none;
        border: 1px solid rgba(0, 255, 255, 1);
      }

      & /deep/ .el-pager li {
        background: none;
        border: 1px solid rgba(0, 255, 255, 1);
      }

      & /deep/ .el-pagination__total,
      & /deep/ .el-pagination__sizes {
        float: left;
        font-size: 1.6rem;
        height: 35px;
        line-height: 35px;
      }

      & /deep/ .el-input__inner {
        /*width:52px;*/
        height: 35px;
      }

      & /deep/ .el-input__icon {
        /*width: 20px;*/
        /*line-height: 3;*/
      }

      & /deep/ .el-input .el-input__inner {
        padding-right: 20px;
        color: #ffffff;
        font-size: 1.6rem;
      }
    }
  }

  .content {
    & /deep/ .el-checkbox__inner {
      height: 20px;
      width: 20px;
      /*background-color: #ffffff;*/
      font-size: 28px;
      font-weight: 400;
      color: rgba(255, 255, 255, 1);
      background: none;
    }

    & /deep/ .el-tooltip__popper {
      background-color: red;
    }

    & /deep/ .el-checkbox__inner::after {
      border-width: 2px;
      /* border-color: black; */
      height: 6px;
      left: 6px;
      top: 3px;
      width: 4px;
    }

    & /deep/ .el-checkbox__input.is-indeterminate .el-checkbox__inner::before {
      top: 8px;
      background: white;
    }
  }

  .line {
    display: inline-flex;
    height: 21px;
    width: 1px;
    background: rgba(0, 255, 255, 1);
    margin: 0 10px;
    vertical-align: middle;
  }

  .dialogBtn {
    font-size: 1.6rem;

    &:hover {
      background: rgba(12, 161, 151, 1);
    }
  }

  .btn {
    cursor: pointer;
    border: none;
    padding: 12px;
    font-size: 16px;
    font-weight: 400;
    color: #ffffff;
    background: rgba(14, 107, 183, 0.9);
    margin-left: 3px;
    border-radius: 3px;
  }
</style>
<style lang="less">
  /**
   * 修改样式   覆盖默认的饿了么全局样式
   */
  .el-form-item__label {
    padding: 0 20px 0 0 !important;
  }
</style>
